<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="keywords" content="">
  <meta name="description" content="">
  <title>用户列表</title>
  <link rel="stylesheet" href="../../../component/pear/css/pear.css">
  <link rel="stylesheet" href="../../../admin/css/dimples.css">
  <link rel="icon" href="../../../admin/images/favicon.ico" type="image/x-icon"/>

  <link rel="stylesheet" href="../../../admin/css/other/user.css"/>
</head>
<body class="pear-container">
<div class="layui-card">
  <div class="layui-card-body">
    <form class="layui-form" action="" lay-filter="user-query-form">
      <div class="layui-form-item">
        <label class="layui-form-label">账号</label>
        <div class="layui-input-inline">
          <input type="text" name="username" placeholder="" class="layui-input">
        </div>
        <label class="layui-form-label">性别</label>
        <div class="layui-input-inline">
          <select name="ssex">
            <option value="0">女</option>
            <option value="1">男</option>
          </select>
        </div>
        <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
          <i class="layui-icon layui-icon-search"></i>
          查询
        </button>
        <button type="reset" class="pear-btn pear-btn-md">
          <i class="layui-icon layui-icon-refresh"></i>
          重置
        </button>
      </div>
    </form>
  </div>
</div>
<div class="user-left user-collasped">
  <div class="layui-card">
    <div class="layui-card-body">
      <div class="button button-primary user-group" user-group=""> 全 部 用 户</div>
      <div class="button button-default user-group" user-group="-1"> 默 认 分 组</div>
      <div style="overflow: auto">
        <ul id="dept-tree" class="dept-tree" data-id="0"></ul>
      </div>
    </div>
  </div>
</div>
<div class="user-main user-collasped">
  <div class="layui-card">
    <div class="layui-card-body">
      <table id="user-table" lay-filter="user-table"></table>
    </div>
  </div>
</div>
</body>

<script type="text/html" id="user-toolbar">
  <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add" {{checkPerms('user:add')}}>
  <i class="pear-icon pear-icon-add"></i>
  新增
  </button>
  <button class="pear-btn pear-btn-md" lay-event="batchRemove" {{checkPerms('user:delete')}}>
  <i class="pear-icon pear-icon-ashbin"></i>
  删除
  </button>
  <button class="pear-btn pear-btn-md" lay-event="collasped">
    <i class="pear-icon pear-icon-modular"></i>
    部门
  </button>
</script>

<script type="text/html" id="user-bar">
  <span {{noPerms('user:delete','user:edit')}} >
  <span class="layui-badge-dot dimples-bg-orange"></span>
  无权限
  </span>

  <a lay-event="edit" {{checkPerms('user:edit')}}>
  <i class="layui-icon dimples-edit-area dimples-blue">&#xe7a4;</i>
  </a>
  <a lay-event="remove" {{checkPerms('user:delete')}}>
  <i class="layui-icon dimples-edit-area dimples-red">&#xe7f9;</i>
  </a>
</script>

<script type="text/html" id="user-status">
  {{#if (d.status == 0) { }}
  <input type="checkbox" name="status" value="{{d.userId}}" lay-skin="switch" lay-text="启用|禁用"
         lay-filter="user-status"/>
  {{# }else if(d.status == 1){ }}
  <input type="checkbox" name="status" value="{{d.userId}}" lay-skin="switch" lay-text="启用|禁用"
         checked lay-filter="user-status"/>
  {{# } }}
</script>

<script type="text/html" id="user-sex">
  {{#if (d.ssex == 1) { }}
  <span>男</span>
  {{# }else if(d.ssex == 0){ }}
  <span>女</span>
  {{# } }}
</script>

<script type="text/html" id="user-login">
  {{#if (d.login == 0) { }}
  <span>在线</span>
  {{# }else if(d.sex == 1){ }}
  <span>离线</span>
  {{# } }}
  <span>未知</span>
</script>

<!-- 依 赖 脚 本 -->
<script src="../../../component/layui/layui.js"></script>
<script src="../../../component/pear/pear.js"></script>
<script src="../../../component/pear/dimples.js"></script>
<script>
  layui.use(['table', 'dtree', 'form', 'jquery', 'popup', 'common', 'ajax', 'loading', 'open'], function () {
    let table = layui.table;
    let form = layui.form;
    let $ = layui.jquery;
    let dtree = layui.dtree;
    let popup = layui.popup;
    let common = layui.common;
    let ajax = layui.ajax;
    let loading = layui.loading;
    let open = layui.open;

    let MODULE_PATH = "/system/user";

    let cols = [
      [
        {type: 'checkbox'},
        {title: 'ID', field: 'userId', align: 'center', width: 100},
        {title: '用户名', field: 'username', align: 'center'},
        {title: '姓名', field: 'realName', align: 'center'},
        {title: '性别', field: 'ssex', align: 'center', templet: '#user-sex', width: 60},
        {title: '部门', field: 'deptName', align: 'center'},
        {title: '邮箱', field: 'email', align: 'center'},
        {title: '电话', field: 'mobile', align: 'center'},
        {
          title: '状态',
          field: 'status',
          align: 'center',
          templet: '#user-status',
          width: 120
        },
        {title: '注册时间', field: 'createTime', align: 'center'},
        {title: '描述', field: 'description', align: 'center'},
        {title: '操作', toolbar: '#user-bar', align: 'center', width: 150}
      ]
    ]

    table.render({
      elem: '#user-table',
      url: BASE_URL + MODULE_PATH,
      headers: {Authorization: getToken()},
      page: true,
      request: {
        pageName: 'pageNumber', //页码的参数名称，默认：page
        limitName: 'pageSize' //每页数据量的参数名，默认：limit
      },
      cols: cols,
      skin: 'line',
      toolbar: '#user-toolbar',
      text: {none: '暂无人员信息'},
      response: {
        statusName: 'code' //规定数据状态的字段名称，默认：code
        , statusCode: 200 //规定成功的状态码，默认：0
        , msgName: 'msg' //规定状态信息的字段名称，默认：msg
        , countName: 'count' //规定数据总数的字段名称，默认：count
        , dataName: 'data' //规定数据列表的字段名称，默认：data
      },
      defaultToolbar: [{
        layEvent: 'refresh',
        icon: 'layui-icon-refresh'
      }, 'filter', 'print', 'exports']
    });

    dtree.render({
      elem: "#dept-tree",
      method: "get",
      url: BASE_URL + "/system/dept",
      headers: {
        Authorization: getToken()
      },
      dataStyle: "layuiStyle",  //使用layui风格的数据格式
      dataFormat: "list",
      line: true,
      skin: "laySimple",
      icon: "-1",
      response: {treeId: "deptId", parentId: "parentId", title: "deptName"},
    });

    dtree.on("node('dept-tree')", function (obj) {
      let field = form.val("user-query-form");
      field.deptId = obj.param.nodeId;
      window.refresh(field);
    });

    $(".user-group").click(function () {
      let group = $(this).attr("user-group");
      let field = form.val("user-query-form");
      if (group === "-1") {
        field.deptId = group;
        $(this).removeClass("button-default");
        $(this).prev().removeClass("button-primary");
        $(this).prev().addClass("button-default");
        $(this).addClass("button-primary");
      } else {
        field.deptId = group;
        $(this).removeClass("button-default");
        $(this).next().removeClass("button-primary");
        $(this).next().addClass("button-default");
        $(this).addClass("button-primary");
      }
      window.refresh(field);
    })

    table.on('tool(user-table)', function (obj) {
      if (obj.event === 'remove') {
        window.remove(obj);
      } else if (obj.event === 'edit') {
        window.edit(obj);
      }
    });

    table.on('toolbar(user-table)', function (obj) {
      if (obj.event === 'add') {
        window.add();
      } else if (obj.event === 'refresh') {
        window.refresh();
      } else if (obj.event === 'batchRemove') {
        window.batchRemove(obj);
      } else if (obj.event == 'collasped') {
        $(".user-left").toggleClass("user-collasped");
        $(".user-main").toggleClass("user-collasped");
        table.resize();
      }
    });

    form.on('submit(user-query)', function (data) {
      window.refresh(data.field);
      return false;
    });

    form.on('switch(user-status)', function (obj) {
      let operate;
      if (obj.elem.checked) {
        operate = "1";
      } else {
        operate = "0";
      }
      loading.Load(1, '');
      ajax.send('/system/user/status', {
        data: JSON.stringify({
          userId: this.value,
          status: operate
        }),
        dataType: 'json',
        contentType: 'application/json',
        type: 'put',
      }, function (result) {
        popup.success(result.msg);
      })
    });

    window.add = function () {
      open.iframe('新增', 'add.html');
    }

    window.edit = function (obj) {
      open.iframe('修改', 'edit.html?userId=' + obj.data['userId']);
    }

    window.remove = function (obj) {
      layer.confirm('确定要删除该用户', {icon: 3, title: '提示'}, function (index) {
        layer.close(index);
        loading.Load(5, '');
        ajax.send(MODULE_PATH + "/" + obj.data['userId'], {
          dataType: 'json',
          type: 'delete',
        }, function (result) {
          popup.success(result.msg, function () {
            obj.del();
          });
        })
      });
    }

    window.batchRemove = function (obj) {
      let ids = common.checkField(obj, 'userId');
      if (common.isEmpty(ids)) {
        popup.warning("未选中数据");
        return false;
      }
      layer.confirm('确定要删除选中用户', {icon: 3, title: '提示'}, function (index) {
        layer.close(index);
        loading.Load(5, '');
        ajax.send(MODULE_PATH + "/batch/" + ids, {
          dataType: 'json',
          type: 'delete',
        }, function (result) {
          popup.success(result.msg, function () {
            table.reload('user-table');
          })
        })
      });
    }

    window.refresh = function (param) {
      table.reload('user-table', {where: param});
    }
  })
</script>
</html>